package com.smedi.ismedi.constructionproject.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSectionConfigOrganization;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionConfigOrganizationRepository;
import com.smedi.ismedi.constructionproject.core.port.adapter.persistence.model.BidSectionConfigOrganizationEntity;
import com.smedi.ismedi.constructionproject.core.port.adapter.persistence.mybatis.mapper.BidSectionConfigOrganizationMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-18 15:54
 */
@Slf4j
@Repository
@AllArgsConstructor
public class BidSectionConfigOrganizationRepositoryImpl extends ServiceImpl<BidSectionConfigOrganizationMapper, BidSectionConfigOrganizationEntity> implements BidSectionConfigOrganizationRepository, IService<BidSectionConfigOrganizationEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public void deleteByBidSectionId(Long bidSectionId) {
        QueryWrapper<BidSectionConfigOrganizationEntity> wrapper = new QueryWrapper();
        wrapper.eq("bid_section_id", bidSectionId);
        this.getBaseMapper().delete(wrapper);
    }

    @Override
    public BidSectionConfigOrganization doInsert(BidSectionConfigOrganization bidSectionConfigOrganization) {
        BidSectionConfigOrganizationEntity bidSectionConfigOrganizationEntity = dozerUtil.convertor(bidSectionConfigOrganization, BidSectionConfigOrganizationEntity.class);
        this.getBaseMapper().insert(bidSectionConfigOrganizationEntity);
        return dozerUtil.convertor(bidSectionConfigOrganizationEntity, BidSectionConfigOrganization.class);
    }

    @Override
    public List<BidSectionConfigOrganization> selectByBidSectionConfigIdOrderById(Long bidSectionConfigId) {
        QueryWrapper<BidSectionConfigOrganizationEntity> wrapper = new QueryWrapper();
        wrapper.eq("bid_section_config_id", bidSectionConfigId)
                .orderByAsc("id");
        List<BidSectionConfigOrganizationEntity> list = this.getBaseMapper().selectList(wrapper);
        return dozerUtil.convertor(list, BidSectionConfigOrganization.class);
    }

    @Override
    public int selectProjectOrgUsedInBidSectionCount(Long projectOrgId) {
        QueryWrapper<BidSectionConfigOrganizationEntity> wrapper = new QueryWrapper();
        wrapper.eq("project_organization_id", projectOrgId);
        return this.getBaseMapper().selectCount(wrapper);
    }
}
